package com.cubeofnine.sugar.activity.export_contact;

import java.util.ArrayList;
import java.util.Iterator;

import com.cubeofnine.android.api.contact.Address;
import com.cubeofnine.android.api.contact.Contact;
import com.cubeofnine.android.api.contact.Email;
import com.cubeofnine.android.api.contact.Organization;
import com.cubeofnine.android.api.contact.Phone;
import com.cubeofnine.sugar.R;
import com.cubeofnine.sugar.business.ContactsBusiness;
import com.cubeofnine.sugar.service.element.ReturnAddContactList;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class SyncUpContactsListThread extends Thread {
	private Context context;
	private ArrayList<Contact> contacts;
	private Handler handler = new Handler();

	public SyncUpContactsListThread(Context context,
			ArrayList<Contact> contacts) {
		super();
		this.context = context;
		this.contacts = contacts;
	}

	public String getClassName() {
		return SyncUpContactsListThread.class.getName();
	}

	@Override
	public void run() {
		Log.i(getClassName(), "run() : Start");

		ReturnAddContactList result = null;
		String errorMessage = "";
		try {
			Iterator<Contact> contactIter = null;
			Contact contact = null;

			String displayName = null;
			String firstName = null;
			String lastName = null;

			ArrayList<Phone> phones = null;
			Iterator<Phone> phoneIter = null;
			Phone phone = null;
			String phoneNumber = null;
			String phoneType = null;

			ArrayList<Email> emails = null;
			Iterator<Email> emailIter = null;
			Email email = null;
			String emailAddress = null;
			String emailType = null;

			ArrayList<Address> addresses = null;
			Iterator<Address> addressIter = null;
			Address address = null;
			String addressType = null;
			String street = null;
			String city = null;
			String state = null;
			String postalCode = null;
			String country = null;

			ArrayList<Organization> organizations = null;
			Iterator<Organization> organizationIter = null;
			Organization organization = null;
			String companyName = null;
			String jobTitle = null;

			com.cubeofnine.sugar.service.element.Contact iContact = null;
			com.cubeofnine.sugar.service.element.Phone iPhone = null;
			com.cubeofnine.sugar.service.element.Email iEmail = null;
			com.cubeofnine.sugar.service.element.Address iAddress = null;
			com.cubeofnine.sugar.service.element.Contacts iContacts = new com.cubeofnine.sugar.service.element.Contacts();

			contactIter = contacts.iterator();
			while (contactIter.hasNext()) {
				contact = contactIter.next();

				{
					displayName = null;

					phoneNumber = null;
					phoneType = null;

					emailAddress = null;
					emailType = null;

					street = null;
					city = null;
					state = null;
					postalCode = null;
					country = null;
					addressType = null;

					companyName = null;
					jobTitle = null;

					iContact = null;
					iPhone = null;
					iEmail = null;
					iAddress = null;
				}

				displayName = contact.getDisplayName();
				firstName = displayName;
				lastName = "";

				try {
					String[] tmp = displayName.split(" ");
					if (tmp.length <= 1) {
						firstName = displayName;
					} else {
						firstName = tmp[0];
						for (int i = 1; i < tmp.length; i++) {
							lastName += tmp[i] + " ";
						}
						lastName = lastName.trim();
					}
				} catch (Exception e) {

				}

				organizations = contact.getOrganization();
				if (organizations != null && !organizations.isEmpty()) {
					organizationIter = organizations.iterator();
					while (organizationIter.hasNext()) {
						organization = organizationIter.next();
						companyName = organization.getOrganization();
						jobTitle = organization.getTitle();
						break;
					}
				}

				Log.d(getClassName(), "firstName = " + firstName);
				Log.d(getClassName(), "lastName = " + lastName);
				Log.d(getClassName(), "companyName = " + companyName);
				Log.d(getClassName(), "jobTitle = " + jobTitle);

				iContact = new com.cubeofnine.sugar.service.element.Contact();
				iContact.setFirst_name(firstName);
				iContact.setLast_name(lastName);
				iContact.setCompany_name(companyName);
				iContact.setJob_title(jobTitle);

				phones = contact.getPhone();
				if (phones != null && !phones.isEmpty()) {
					phoneIter = phones.iterator();
					while (phoneIter.hasNext()) {
						iPhone = new com.cubeofnine.sugar.service.element.Phone();
						phone = phoneIter.next();
						phoneNumber = phone.getNumber();
						phoneType = phone.getType();

						iPhone.setPhone_type(phoneType);
						iPhone.setPhone_value(phoneNumber);

						Log.d(getClassName(), "phoneType = " + phoneType);
						Log.d(getClassName(), "phoneNumber = " + phoneNumber);

						iContact.addPhone(iPhone);
					}
				}

				emails = contact.getEmail();
				if (emails != null && !emails.isEmpty()) {
					emailIter = emails.iterator();
					while (emailIter.hasNext()) {
						iEmail = new com.cubeofnine.sugar.service.element.Email();
						email = emailIter.next();
						emailAddress = email.getAddress();
						emailType = email.getType();

						iEmail.setEmail_type(emailType);
						iEmail.setEmail_value(emailAddress);

						Log.d(getClassName(), "emailType = " + emailType);
						Log.d(getClassName(), "emailAddress = " + emailAddress);

						iContact.addEmail(iEmail);
					}
				}

				addresses = contact.getAddresses();
				if (addresses != null && !addresses.isEmpty()) {
					addressIter = addresses.iterator();
					while (addressIter.hasNext()) {
						iAddress = new com.cubeofnine.sugar.service.element.Address();
						address = addressIter.next();
						street = address.getStreet();
						city = address.getCity();
						state = address.getState();
						postalCode = address.getPostalCode();
						country = address.getCountry();
						addressType = address.getType();

						iAddress.setAddress_type(addressType);
						iAddress.setStreet(street);
						iAddress.setCity(city);
						iAddress.setState(state);
						iAddress.setPostalcode(postalCode);
						iAddress.setCountry(country);

						Log.d(getClassName(), "addressType = " + addressType);
						Log.d(getClassName(), "street = " + street);
						Log.d(getClassName(), "city = " + city);
						Log.d(getClassName(), "state = " + state);
						Log.d(getClassName(), "postalCode = " + postalCode);
						Log.d(getClassName(), "country = " + country);

						iContact.addAddress(iAddress);
					}

				}

				iContacts.addContact(iContact);
			}

			Log.d(getClassName(), "iContacts = " + iContacts.toString());

			ContactsBusiness contactsBusiness = new ContactsBusiness(context);
			result = contactsBusiness.addContactList(iContacts.getContacts());
		} catch (Exception e) {
			Log.e(getClassName(), "Connection Error", e);
			errorMessage = context.getString(R.string.msg_connection_error);
		} finally {
			Log.d(getClassName(), "Finally");
			handler.post(new SyncUpContactsListRunnable(context, result,
					errorMessage));
		}

		Log.i(getClassName(), "run() : End");
	}
}
